#! /bin/sh
### BEGIN INIT INFO
# Short-Description: AnyDesk global service
### END INIT INFO

DESC="AnyDesk global service"
PRGNAM=anydesk
DAEMON=/usr/bin/$PRGNAM
OPTS="--service"
PID=/var/run/$PRGNAM.pid

# Gracefully exit if the package has been removed.
test -x $DAEMON || exit 0


#
#	Function that starts the daemon/service.
#
anydesk_start(){
  if [ -s $PID ]; then
    echo "$DESC is already running: $(cat $PID)"
    exit 1
  fi

  if [ -x $DAEMON ]; then
    echo "Starting $DESC"
    $DAEMON -- $OPTS &
    pidof $DAEMON > $PID
  fi
}
#
#	Function that stops the daemon/service.
#
anydesk_stop()
{
  if [ -e $PID ]; then
    kill $(cat $PID)
    killall $PRGNAM
    rm -rf $PID
    echo "$DESC has been stopped."
  else
    echo "$DESC is not running."
  fi
}

#
# Function that shows the current status of the daemon/service.
#
anydesk_status()
{
  if [ -s $PID ]; then
    echo "$DESC is running: $(cat $PID)"
  else
    echo "$DESC is not running."
  fi
}


case "$1" in
  start)
	anydesk_start
	;;
  stop)
	anydesk_stop
	;;
  restart|force-reload)
	anydesk_stop
	sleep 2
	anydesk_start
	;;
  status)
	anydesk_status
	;;
  *)
	echo "Usage: $0 {start|stop|restart|force-reload|status}" >&2
	exit 1
	;;
esac

exit 0
